package TAIC.Classifier;
import java.io.*;
import java.util.*; 

import TAIC.test.TestMode;

public class SVM extends Classifier {
	public static int MaxClass = TestMode.para.getParaInt( "MaxClass" ) ;

	public boolean pipe = false ;
	
	@Override
	public boolean isPipe() {
		return false ; 
	}
	
	@Override
	public void train(String trainFile) {
	}	
	
	public double test(String testFile) {
		if ( MaxClass == 2 ) return test2 ( testFile ) ;
		else return testM ( testFile) ;
	}
	
	public double test2(String testFile) {	
		try { Runtime.getRuntime().exec ( "svm.bat > temp.txt").waitFor() ; } 
		catch ( Exception e ) {e.printStackTrace();}
		return evaluate2();
	}

	public double testM(String testFile) {	
		try { Runtime.getRuntime().exec ( "svm_m.bat > temp.txt").waitFor() ; } 
		catch ( Exception e ) {e.printStackTrace();}
		return evaluateM();
	}
	
	double evaluate2 () {
		int all = 0 ;
		int correct = 0 ;
		try {
			Scanner f1 = new Scanner ( new File ( "out.txt" ) ) ;
			Scanner f2 = new Scanner ( new File ( "testset" ) ) ;
			while ( f1.hasNextDouble () ) {
				double x = f1.nextDouble() ;
				int y = ( f2.next().charAt ( 0 ) == '-' ? -1 : +1) ;
				f2.nextLine () ;
				if ( x * y > 0 ) correct ++ ;
				all ++ ;
			}
		}
		catch ( Exception e ) { e.printStackTrace () ; }
		return correct / (double) all ; 
	}

	double evaluateM () {
		int all = 0 ;
		int correct = 0 ;
		try {
			Scanner f1 = new Scanner ( new File ( "out.txt" ) ) ;
			Scanner f2 = new Scanner ( new File ( "testset" ) ) ;
			while ( f1.hasNextInt () ) {
				int x = f1.nextInt() ;
				int y = f2.nextInt() ;
				f1.nextLine () ;
				f2.nextLine () ;
				if ( x == y  ) correct ++ ;
				all ++ ;
			}
		}
		catch ( Exception e ) { e.printStackTrace () ; }
		return correct / (double) all ; 
	}
}
